/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jserver.gameserver.custom;

import java.io.File;
import java.util.Map;

import javax.xml.parsers.DocumentBuilderFactory;

import javolution.util.FastMap;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.l2jserver.gameserver.datatables.ItemTable;
import com.l2jserver.gameserver.model.L2ItemInstance;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.serverpackets.ExShowScreenMessage;
import com.l2jserver.gameserver.network.serverpackets.PlaySound;
import com.l2jserver.gameserver.templates.StatsSet;

/**
 * @author Laikeriz
 *
 */
public class SpreeManager
{
	public static Map<Integer, StatsSet> sprees = new FastMap<Integer, StatsSet>();
	
	public static void executeSpreeIfAvailable(L2PcInstance player)
	{
		if (sprees.containsKey(player.continousKills))
		{
			StatsSet set = sprees.get(player.continousKills);
			String msg = set.getString("message");
			if (msg.contains("%player%"))
				msg = msg.replace("%player%", player.getName());
			player.broadcastPacket(new ExShowScreenMessage(msg, 5000));
			player.broadcastPacket(new PlaySound("Announcer." + set.getString("sound")));
			player.setPkKills(player.getPkKills() + 1);
			player.updatePvpPkTitle();
			CustomConfig config = CustomConfig.getInstance();
			if (config.SPREE_REWARD_ENABLED)
			{
				L2ItemInstance reward = ItemTable.getInstance().createItem("spree_reward", config.SPREE_REWARD_ID, config.CRYSTAL_REWARD_COUNT, null);
				player.addItem("spree_reward", reward, null, true);
			}
		}
	}
	
	public static void init()
	{
		try
		{
			File file = new File("config/spree.xml");
			Document doc = null;
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setValidating(false);
			factory.setIgnoringComments(true);
			doc = factory.newDocumentBuilder().parse(file);
			for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
			{
				if ("spreeList".equalsIgnoreCase(n.getNodeName()))
				{
					for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
					{
						if ("spree".equalsIgnoreCase(d.getNodeName()))
						{
							StatsSet tempSet = new StatsSet();
							int killsCount = Integer.parseInt(d.getAttributes().getNamedItem("killsCount").getNodeValue());
							tempSet.set("sound", d.getAttributes().getNamedItem("sound").getNodeValue());
							tempSet.set("killsCount", killsCount);
							tempSet.set("message", d.getAttributes().getNamedItem("message").getNodeValue());
							sprees.put(killsCount, tempSet);
						}
					}
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
